<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="客户名称" prop="cName">
        <el-input
          v-model="queryParams.cName"
          placeholder="请输入客户名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
     
      <el-form-item label="经办人" prop="jbrId">
        <my-jbr v-model="queryParams.jbrId" />
      </el-form-item>
     
      <el-form-item label="账户类型" prop="dictCode">
        <my-dictdata v-model="queryParams.dictCode" dictDataType="ClientType" dictDataName="请选择账户类型" />
      </el-form-item>

      <el-form-item label="业务员" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请选择业务员"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      
      
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:client:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:client:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:client:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:client:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" @row-dblclick="dbSelected" :data="clientList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="客户名称" align="center" prop="cName" width="220px" />
      <el-table-column label="客户简称" align="center" prop="cJc" />
      <el-table-column label="经办人" align="center" prop="erpJingbanren.jbrName" />
      <el-table-column label="账户类型" align="center" prop="sysDictData.dictLabel" />
      <el-table-column label="业务员" align="center" prop="sysUser.nickName" />
      <el-table-column label="联系人" align="center" prop="cPtcontact" />
      <el-table-column label="联系电话" align="center" prop="cPhone" />
      <el-table-column label="是否本公司" align="center" prop="cIsmysoftStr" />
      <el-table-column label="是否协议户" align="center" prop="cIsxieyiStr" />
      <el-table-column label="维护人" align="center" prop="cUser.nickName" />
      <el-table-column label="维护时间" align="center" prop="cTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.cTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:client:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:client:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <!-- 添加或修改客户对话框 -->
    <my-dialog :title="title" TimeoutNumber="2000" :visible.sync="open" width="500px">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="客户名称" prop="cName">
          <el-input v-model="form.cName" maxlength="120" show-word-limit size="small" placeholder="请输入客户名称" />
        </el-form-item>

        <el-form-item label="客户简称" prop="cJc">
          <el-input v-model="form.cJc" maxlength="6" show-word-limit size="small" placeholder="请输入客户简称" />
        </el-form-item>

        <el-form-item label="经办人" prop="jbrId">
          <my-jbr v-model="form.jbrId" style="width:100%" />
        </el-form-item>

        <el-form-item label="账户类型" prop="dictCode">
          <my-dictdata v-model="form.dictCode" style="width:100%" dictDataType="ClientType" dictDataName="请选择账户类型" />
        </el-form-item>

        <el-form-item label="业务员" prop="nickName">
          <el-input placeholder="请选择业务员" disabled v-model="form.nickName" class="input-with-select" style="width:100%">
            <el-button slot="append" icon="el-icon-search" @click="selectUser"></el-button>
          </el-input>
        </el-form-item>

        <el-form-item label="联系人" prop="cPtcontact">
          <el-input v-model="form.cPtcontact" maxlength="8" show-word-limit size="small" placeholder="请输入联系人" />
        </el-form-item>

        <el-form-item label="联系电话" prop="cPhone">
          <el-input v-model="form.cPhone" maxlength="11" show-word-limit size="small" placeholder="请输入联系电话" />
        </el-form-item>

        <el-form-item label="是否本公司" prop="cIsmysoft">
          <el-radio-group v-model="form.cIsmysoft">
            <el-radio-button label="0">否</el-radio-button>
            <el-radio-button label="1">是</el-radio-button>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="是否协议户" prop="cIsxieyi">
          <el-radio-group v-model="form.cIsxieyi">
            <el-radio-button label="0">否</el-radio-button>
            <el-radio-button label="1">是</el-radio-button>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>

      <!-- 第二个弹出层 -->
      <my-dialog :title="title2" :visible.sync="open2" append-to-body width="1000px" height="600px" :close-on-click-modal="false">
        <my-select-user v-model="form.userId" :isOpen.sync="open2" :userId.sync="form.userId" :nickName.sync="form.nickName"   />
      </my-dialog>
    </my-dialog>
  </div>
</template>

<script>
import { listClient, getClient, delClient, addClient, updateClient, exportClient } from "@/api/erp/system/client";
import { getUserProfile } from '@/api/system/user';

export default {
  name: "Client",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 遮罩层2
      loading2: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      whuserId: 0,
      // 客户表格数据
      clientList: [],
      // 弹出层标题
      title: "",
      // 弹出层标题2
      title2: "",
      // 是否显示弹出层
      open: false,
      // 是否显示弹出层
      open2: false,
      // 查询参数
      queryParams: {
        cName: null,
        jbrId: null,
        dictCode: null,
        userId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        cName: [
          { required: true, message: "客户名称不能为空", trigger: "blur" }
        ],
        jbrId: [
          { required: true, message: "经办人不能为空", trigger: "blur" }
        ],
        cJc: [
          { required: true, message: "客户简称不能为空", trigger: "blur" }
        ],
        dictCode: [
          { required: true, message: "账户类型", trigger: "blur" }
        ],
        nickName: [
          { required: true, message: "业务员不能为空", trigger: "blur" }
        ],
        cPtcontact: [
          { required: true, message: "联系人不能为空", trigger: "blur" }
        ],
        cPhone: [
          { required: true, message: "联系电话不能为空", trigger: "blur" }
        ],
        cIsmysoft: [
          { required: true, message: "是否本公司不能为空", trigger: "blur" }
        ],
        cIsxieyi: [
          { required: true, message: "是否协议户不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();

    getUserProfile().then(response => {
        this.whuserId = response.data.userId;
    });
  },
  methods: {

    /** 查询客户列表 */
    getList() {
      this.loading = true;
      listClient(this.queryParams).then(response => {
        this.clientList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    dbSelected(row){
      console.log(row);
    },
    // 数据字典，账户类型 clientType字典翻译
    dictCodeFormat(row, column) {
      return this.selectDictLabel(this.dictCodeOptions, row.dictCode);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        cId: null,
        cName: null,
        jbrId: null,
        cJc: null,
        dictCode: null,
        userId: null,
        nickName : null,
        cPtcontact: null,
        cPhone: null,
        cIsmysoft: 0,
        cIsxieyi: 0,
        cUserId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.cId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加客户";
    },
    selectUser(){
      this.open2 = true;
      this.title2 = "选择用户";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const cId = row.cId || this.ids
      getClient(cId).then(response => {
        setTimeout(() => {
          this.form = response.data;
          this.form.nickName = response.data.sysUser.nickName;
        }, this.setTimeoutNumber);
        this.open = true;
        this.title = "修改客户";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        console.log(this.form);
        if (valid) {
          if (this.form.cId != null) {
            console.log(this.form);
            updateClient(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.cUserId = this.whuserId;
            addClient(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const cIds = row.cId || this.ids;
      this.$confirm('是否确认删除客户编号为"' + cIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delClient(cIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有客户数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportClient(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
